﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class ToaThuocDAO
    {
        public static DataTable LayDanhSachToaThuoc()
        {
            string sql;
            sql = "SELECT * FROM [ToaThuoc]";
            return DataAccess.ExecQuery(sql);
        }

        public static int ThemToaThuoc(ToaThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"INSERT INTO [ToaThuoc](TongTien, MaPKB) VALUES({0}, {1})", obj.TongTien, obj.MaPKB);
            DataAccess.ExecNonQuery(sql);

            sql = "select max(MaTT) from ToaThuoc";
            DataTable dt = DataAccess.ExecQuery(sql);
            int max = Convert.ToInt32(dt.Rows[0][0]);
            return max;
        }

        public static void SuaToaThuoc(ToaThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"UPDATE [ToaThuoc] SET TongTien = {0}, TongTien = {1} WHERE MaTT = {2}", obj.TongTien, obj.MaPKB, obj.MaTT);
            DataAccess.ExecNonQuery(sql);
        }

        public static void XoaToaThuoc(int maToaThuoc)
        {
            string sql;
            sql = string.Format("DELETE [ToaThuoc] WHERE MaTT = {0}", maToaThuoc);
            DataAccess.ExecNonQuery(sql);
        }

        public static ToaThuocDTO LayThongTinToaThuocTheoMaToaThuoc(int maToaThuoc)
        {
            ToaThuocDTO kq = new ToaThuocDTO();
            string sql;
            sql = @"select * FROM [ToaThuoc] where MaTT = " + maToaThuoc;
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                kq.MaTT = Convert.ToInt32(dt.Rows[0]["MaTT"]);
                kq.TongTien = Convert.ToDouble(dt.Rows[0]["TongTien"]);
                kq.MaPKB = Convert.ToInt32(dt.Rows[0]["MaPKB"]);
            }
            return kq;
        }
    }
}
